function ProgressPublicProject() {
    var _this = this;
    var page,pageNum = 1,page_size = 10;
    var projectType,projectName;
    var companyName = '根节点';

    _this.init = function (page1) {
        page = page1,
        projectType = $("#projectType").val();
        projectName = $("#projectName").val();
        _this.initTree();
        _this.initEvent();
        _this.initTable();
        businessType.init();
        regionTree.init();
    }

    var tree_options = {
        id: 'company_tree',
        url: appPath + '/popup/tree4AllCompany?projectId='+$("#projectId").val(),
        setting: {
            view: {
                dblClickExpand: false,
                fontCss: getFontCss
            },
            callback: {
                onClick: function (e, treeId, treeNode) {
                    if( treeNode.show ){
                        _this.onClick(e, treeId, treeNode);
                    }
                }
            }
        },
        zNodes: '',
        rMenu: ''
    };


    //初始化树
    _this.initTree = function(){
        $.ajax({
            url: tree_options.url,
            async: false,
            type: 'post',
            data: {},
            success: function(data){
                tree_options.zNodes = eval(data.result);
                // 加载树
                $.fn.zTree.init($("#"+tree_options.id), tree_options.setting, tree_options.zNodes);
                //默认选中根节点
                if($("#companyId").val() == ''){
                    var rootNode = _this.getRoots()[0];
                    var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
                    treeObj.selectNode(rootNode); // 默认选中根节点
                    treeObj.expandNode(rootNode, true, false, false);// 默认展开根节点
                    $("#companyId").val(rootNode.id);
                }
            }
        });
    };

    // 获取根节点集合
    _this.getRoots = function() {
        var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
        return treeObj.getNodesByFilter(function(node){return node.level==0;});
    };

    //点击
    _this.onClick = function(e,treeId, treeNode) {
        $("#companyId").val(treeNode.id);
        companyName = treeNode.name;
        _this.freshTable();
    };

    // 初始化事件
    _this.initEvent = function () {
        $("#search_btn").click(function () {
            _this.initTable();
        });

        $('#select-input').click(function () {
            $("#search_tree").show();
            $("#search_tree").fadeIn();
        })
        $('#search_tree').mouseleave(function(){
            $("#search_tree").hide();
        })

    }

    // 搜索全部
    _this.searchAll = function () {
        $("#businessIds").val("");
        $("#regionIds").val("");
        $("#companyName").val("");
        $("#businessId").val("");
        $("#company_business").val("");
        $("#categoryId").val("");
        $("#company_category").val("");
        $("#company_region").val("");
        $("#regionId").val("");
        businessType.init();
        regionTree.init();
        _this.initTable();
    }

    // 导出
    _this.exportProgress = function () {
        var date = new Date();
        var seperator = "/";
        var currentDate =date .getFullYear()+seperator+date.getMonth()+seperator+date.getDay();
        // 文件名
        var fileName = $("#projectName").val()+"-"+companyName+"-进度表"+ currentDate + ".xlsx";
        var params = $("#companyListForm").serialize();
        window.location.href = appPath + '/super/excelExport/progressExport?'+ params+"&fileName=" + fileName;
    }

    // 单位列表
    _this.initTable = function () {
        var list_url = appPath + "/super/project_public/projectProgressList";
        page.init("companyListForm", list_url, "company_list", "company_page", pageNum, page_size);
        page.goPage(1);
        page.list = function(dataList){
            var inner = "", item;
            if(dataList){
                var len = dataList.length;
                for(var i=0; i< len; i++) {
                    item = dataList[i];
                    inner += '<tr >';
                    inner += '<td>' + (i + 1) + '</td>';
                    inner += '<td>' + item['companyName'] + '</td>';
                    inner += '<td>' +  item['personCount'] + '</td>';
                    inner += '<td>'+ _this.calculateTrainCompleteRate(item['sumRequireStudyTime'], item['sumTotalStudyTime']) + '</td>';
                    inner += '<td>'+ _this.calculateQuestionCorrectRate(item['sumCorrectRate'], item['personCount']) + '</td>';
                    inner += '<td>'+ _this.calculateExamPassRate(item['examPassTotal'], item['personCount']) + '</td>';
                    inner += '</tr>';
                }
            }
        return inner;
        }
    }

    // 计算培训完成率
    _this.calculateTrainCompleteRate = function (sumRequireStudyTime, sumTotalStudyTime) {
        // 不含培训则返回  " / "
        if (_this.isContains('236', projectType)) {
            return '/'
        }
        if(sumRequireStudyTime == 0){
            return '0.00%';
        }
        var res = sumTotalStudyTime*100/(sumRequireStudyTime*60);
        res = res.toFixed(2);
        return res+'%';
    };

    // 计算答题正确率
    _this.calculateQuestionCorrectRate = function (sumCorrectRate, personCount) {
        // 不含练习则返回  " / "
        if (_this.isContains('135', projectType)) {
            return '/'
        }
        if(personCount == 0){
            return '0.00%';
        }
        var res =sumCorrectRate/personCount;
        res = res.toFixed(2);
        return res+'%';
    }

    // 计算考试合格率
    _this.calculateExamPassRate = function (examPassTotal, personCount) {
        // 不含考试则返回  " / "
        if (_this.isContains('124', projectType)) {
            return '/'
        }
        if(personCount == 0){
            return '0.00%';
        }
        var res = examPassTotal*100/personCount;
        res = res.toFixed(2);
        return res+'%';
    }

    // 勾选刷新列表
    _this.freshTable = function () {
        if($("#companyId").val() == ""){
            return;
        }
        // 是否显示子节点人员
        if($("#recursion").is(":checked")){
            $("#type").val(1);
        }else{
            $("#type").val(2);
        }
        _this.initTable();
    }

    //判断字符串是否包含某字符
    _this.isContains = function (str, substr) {
        return new RegExp(substr).test(str);
    }

}

var getFontCss = function(treeId, node){
    if( node.show ){
        return node.font = {'color':'#333!important'};
    }else{
        return node.font = {'color':'#ccc!important','background':'none!important',};
    }
}

var progressPublicProject = new ProgressPublicProject();